package com.lixin.smartclassroom.ui.home.tab5.activity

import android.graphics.Bitmap
import android.graphics.Color
import com.drake.net.utils.scopeNetLife
import com.lixin.smartclassroom.R
import com.lixin.smartclassroom.api.addclassnews
import com.lixin.smartclassroom.api.uploadFiles
import com.lixin.smartclassroom.common.saveSuccessDialog
import com.lixin.smartclassroom.databinding.ActivityReleaseNoticeBinding
import com.lixin.smartclassroom.ui.home.adapter.selectImages
import com.lixin.smartclassroom.ui.home.tab1.activity.TeacherSendMessageAct
import com.lixinkeji.base.BaseActivity
import com.lixinkeji.extension.getIntentString
import com.lixinkeji.extension.onClick
import com.lixinkeji.extension.start
import com.lixinkeji.extension.text
import com.lixinkeji.extension.toast
import com.lixinkeji.utils.AppUtil
import com.lixinkeji.utils.NativeUtil
import com.lixinkeji.utils.SoftKeyBroadManager
import com.luck.picture.lib.entity.LocalMedia
import java.io.File

/**
 * Description 发布通知
 * Author: lwc
 * Date:2023/12/12 15:40
 */
class ReleaseNoticeAct : BaseActivity<ActivityReleaseNoticeBinding>() {
    override fun getViewBinding() = ActivityReleaseNoticeBinding.inflate(layoutInflater)

    override fun topBarHide() = false
    override fun startPadding() = true
    override fun title() = "发布通知"
    override fun textMenuContent() = "历史通知"
    override fun textMenuColor(): Int {
        return Color.parseColor("#666666")
    }
    private var classid = ""
    override fun onTextMenuListener() {
        start(TeacherSendMessageAct::class.java) {
            putExtra("type", "2")
            putExtra("classid", classid)
        }
    }

    private var localMediaList = ArrayList<LocalMedia>()
    private var hasSelectType = 1
    private var imgList = ArrayList<String>()
    private var files = ArrayList<File>()
    private var imgListUrl = ArrayList<String>()
    var size = 0

    override fun iniView() {
        classid = getIntentString("classid")
        binding.apply {
            hideParentSoftKeyborad(R.id.scrollView)
            binding.selectImageVideo.selectImages(
                localMediaList,
                this@ReleaseNoticeAct,
                hasSelectType,
                9,
                1,
                true,
                onClick = {
                    AppUtil.hideSoftKeyboard(this@ReleaseNoticeAct)
                }) { it ->
                size = it.size
                imgList.clear()
                localMediaList.clear()
                localMediaList.addAll(it)
            }
        }
    }


    override fun onClick() {
        binding.apply {
            btnSave.onClick {
                if (etTitle.text().isEmpty()) {
                    toast("请输入通知标题")
                    return@onClick
                }
                if (etContent.text().isEmpty()) {
                    toast("请输入通知内容")
                    return@onClick
                }
                if(localMediaList!=null&&localMediaList.size>0){
                    scopeNetLife {
                        for (i in 0 until localMediaList.size) {
                            val filePath: String = localMediaList[i].realPath
                            val bitmap: Bitmap =
                                NativeUtil.compressBitmap(this@ReleaseNoticeAct, filePath)
                            val file: File = NativeUtil.bitmapToFile(
                                this@ReleaseNoticeAct,
                                bitmap,
                                60,
                                System.currentTimeMillis().toString()
                            )
                            files.add(file)
                        }
                        val baseBean = uploadFiles(files)
                        if (baseBean.result == "0") {
                            imgListUrl.addAll(baseBean.urls!!)
                            var imageUrl = ""
                            for (i in 0 until imgListUrl.size) {
                                if (i == 0) {
                                    imageUrl = imgListUrl[i]
                                } else {
                                    imageUrl += "," + imgListUrl[i]
                                }
                            }
                            val baseBean = addclassnews(classid,etTitle.text(), etContent.text(), imageUrl)
                            if (baseBean.result == "0") {
                                saveSuccessDialog("发布成功")
                                finish()
                            }else{
                                toast(baseBean.resultNote.toString())
                            }
                        }
                    }
                }else{
                    scopeNetLife {
                        val baseBean = addclassnews(classid,etTitle.text(), etContent.text(),"")
                        if (baseBean.result == "0") {
                            saveSuccessDialog("发布成功")
                            finish()
                        }else{
                            toast(baseBean.resultNote.toString())
                        }
                    }

                }

            }
        }
    }
}